import time


class HtmlOutputer(object):
    def __init__(self):
        self.datas = []
        self.img_datas = []
        self.title_datas = []

    def collect_data(self, data):
        if data is None:
            return
        self.img_datas.append(data['img_nodes'])
        self.title_datas.append(data["titles"])
        self.datas.append(data)

    def output_Html(self):
        file_name = time.strftime("%Y_%m_%d_%H:%M:%S")
        with open("out.html", "w",encoding='utf-8') as f_out:
            f_out.write("<!DOCTYPE html>")
            f_out.write('<html lang="en">')
            f_out.write(r'<head>')
            f_out.write(r'<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>')
            f_out.write(r'</head>')
            f_out.write("<body>")
            f_out.write(r'<table class="table table-bordered table-hover">')

            item_css = ['active', 'success', 'warning', 'info']
            for data in self.img_datas:
                index = self.datas.index(data) % len(item_css)
                f_out.write(r'<tr class="' + item_css[index] + r'">')
                f_out.write('<td>%s</td>' % data)
                f_out.write("</tr>")
            for data in self.title_datas:
                index = self.datas.index(data) % len(item_css)
                f_out.write(r'<tr class="' + item_css[index] + r'">')
                f_out.write('<td>%s</td>' % data)
                f_out.write("</tr>")
            f_out.write("</table>")
            f_out.write("</body>")
            f_out.write("</html>")